package org.example.pcbcircuitboard.service;

import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.Data;
import org.eclipse.paho.client.mqttv3.IMqttClient;
import org.eclipse.paho.client.mqttv3.MqttMessage;
import org.example.pcbcircuitboard.pojo.FailureMaxMinPojo;
import org.example.pcbcircuitboard.pojo.PowerSupplyPojo;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 封装电源命令数据
 */
@Service
public class PowerSupplyService {
    private String topic="pcb/control";
    /**
     * 封装数据 | 下发电源命令数据
     * @param powerSupplyPojo 电源命令数据
     * @param mqttClient mqtt客户端
     * @return  状态结果
     */
    public boolean encapsulationMqttData(PowerSupplyPojo powerSupplyPojo, IMqttClient mqttClient){
        try {
            List<Integer> defectTypeList = new ArrayList<>();
            List<Integer> slotMappingList = new ArrayList<>();
            for ( FailureMaxMinPojo failureMaxMinPojo : powerSupplyPojo.getSlotRange()){
                defectTypeList.add(Integer.parseInt(failureMaxMinPojo.getIndex()));
            }
//            // TODO 初始化 slotMappingList 为 15 个 0
//            for (int i = 0; i < 15; i++) {
//                slotMappingList.add(0);
//            }
//
//            // TODO 遍历 slotRange，填充 slotMappingList
//            for (FailureMaxMinPojo failureMaxMinPojo : powerSupplyPojo.getSlotRange()) {
//                int defectType = Integer.parseInt(failureMaxMinPojo.getIndex());
//                int min = Integer.parseInt(failureMaxMinPojo.getMin()) - 1; // 转换为 0-based 索引
//                int max = Integer.parseInt(failureMaxMinPojo.getMax()) - 1; // 转换为 0-based 索引
//
//                // TODO 填充对应的槽位
//                for (int i = min; i <= max; i++) {
//                    if (i >= 0 && i < 15) { // 确保索引在范围内
//                        slotMappingList.set(i, defectType);
//                    }
//                }
//            }
            // TODO 操作类型
            String control = powerSupplyPojo.getPowerOn();
            // TODO 产品信息
            String model = powerSupplyPojo.getSelectedProducts().get(0).getName();
            String batch = "000A1";
            String serial = "00008899";
            // TODO 整合封装数据 MqttMessage
            PowerCommand powerCommand = new PowerCommand(defectTypeList, slotMappingList, control, model, batch, serial);
            ObjectMapper mapper = new ObjectMapper();
            String json = mapper.writeValueAsString(powerCommand);
            MqttMessage mqttMessage =new MqttMessage(json.getBytes());
            // TODO 配置消息
            mqttMessage.setQos(1);
            mqttMessage.setRetained(false);
            mqttClient.publish(topic, mqttMessage);
            // TODO 返回封装结果
            return true;
        }catch (Exception e){
            System.out.println(e.getMessage());
            return false;
        }


    }

    /**
     * 最终数据
     */
    public static class PowerCommand {
        // 故障列表
        @JsonProperty("defect_types")
        private List<Integer> defectTypes;
        // 槽位配置
        @JsonProperty("slot_mapping")
        private List<Integer> slotMapping;
        // 控制命令
        @JsonProperty("control")
        private String control;
        // 产品信息
        @JsonProperty("product")
        private Product product;
        public PowerCommand(List<Integer> defectTypes, List<Integer> slotMapping, String control, String model, String batch, String serial) {
            this.defectTypes = defectTypes;
            this.slotMapping = slotMapping;
            this.control = control;
            this.product = new Product(model, batch, serial);
        }
    }

    /**
     * 产品信息
     */
    @Data
    private static class Product {
        // 产品型号
        @JsonProperty("model")
        private String model;
        // 产品批次
        @JsonProperty("batch")
        private String batch;
        // 序列号
        @JsonProperty("serial")
        private String serial;

        public Product(String model, String batch, String serial) {
            this.model = model;
            // 获取当前日期
            Date date = new Date();
            SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
            String today = sdf.format(date);
            this.batch = "SN"+today;
            // 获取
            this.serial = today+"1";
        }
    }
}
